/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import helper.NewHibernateUtil;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import pojo.Added;
import pojo.Bill;
import pojo.BillHistory;
import pojo.BillProduct;
import pojo.CuahangProduct;
import pojo.CuahangProductId;
import pojo.Store;

/**
 *
 * @author Tuan
 */
public class DAOBill {

    private Session session;

    public DAOBill() {
        SessionFactory factory = NewHibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }

    public boolean saveBill(Bill bill,List<BillProduct> dshang,List<CuahangProduct> dscuahangpro) {
        try {
            session.getTransaction().begin();
            session.save(bill);
            Criteria c = session.createCriteria(Bill.class);
            c.addOrder(Order.desc("id"));
            c.setMaxResults(1);
            Bill result = (Bill)c.uniqueResult();
            for(int i=0;i<dshang.size();i++)
            {
                BillProduct billPro = dshang.get(i);
                billPro.setIdBill(result.getId());
                session.save(billPro);
            }
            BillHistory billhis = new BillHistory();
            billhis.setDtime(new Date());
            billhis.setPerAccount(bill.getPerAccount());
            billhis.setAction("Tạo và in hóa đơn");
            billhis.setBill(result);
            session.save(billhis);
            session.flush();
            session.getTransaction().commit();
            DAOCuaHangProduct daocuahangpro = new DAOCuaHangProduct();
            for(int i=0;i<dscuahangpro.size();i++)
            {
                CuahangProductId cuahangproid = new CuahangProductId(dscuahangpro.get(i).getId().getIdCuahang(),dscuahangpro.get(i).getId().getIdProduct());
                daocuahangpro.getAndUpdate(cuahangproid, dscuahangpro.get(i).getCount());
            }
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }
  public Bill getBillById(int id)
    {
         try{
            session.getTransaction().begin();
            String sql = "from Bill where id = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            Bill result = (Bill)query.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
 public List<Bill> getAllBill()
    {
         try{
            session.getTransaction().begin();
            String sql = "from Bill";
            Query query = session.createQuery(sql);
            List<Bill> result = (List<Bill>)query.list();
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
   /* public boolean saveBillDetails(List<BillProductId> list) {
        try {
            session.getTransaction().begin();
            session.save(list.get(1));
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }*/
}
